/*
 * test_main.c
 *
 *  Created on: 2011-7-16
 *      Author: minorityee1984@gmail.com
 *     Purpose: for testing
 */

//#ifdef TEST

#include "protocol.h"
#include "code_engine.h"

#include <stdio.h>
#include <string.h>
#include <assert.h>



#define MAX_BUF_LEN 65535
Byte buf[MAX_BUF_LEN] = {0};

static void test_protocol();

int main()
{
	test_protocol();
	return 0;
}

void test_protocol()
{
	int	ret = 0;
	Message msg1 = {{0}};
	msg1.header.msgID = MSG_CMD_ACTIVATE_ROTATE;
	msg1.body.msgActivateRotate.turnOn = 1;
	msg1.body.msgActivateRotate.degree = 0xff + 3;

	Message msg2 = {{0}};
	msg2.header.msgID = MSG_CMD_ACTIVATE_ROTATE;
	msg2.body.msgActivateRotate.turnOn = 2;
	msg2.body.msgActivateRotate.degree = 0xff + 4;

	init_code_engine();

	unsigned int offset = 0;
	unsigned int uBufLen = MAX_BUF_LEN;
	Byte *pszBuf = buf;
	ret = pack_msg(pszBuf, uBufLen, &offset, 1, &msg1, GET_MSG_SIZE(MsgActivateRotate));
	if (0 > ret)
	{
		assert(!"pack_msg msg1 failed!");
	}

	pszBuf += offset;
	uBufLen -= offset;
	ret = pack_msg(pszBuf, MAX_BUF_LEN, &offset, 2, &msg2, GET_MSG_SIZE(MsgActivateRotate));
	if (0 > ret)
	{
		assert(!"pack_msg msg2 failed!");
	}


	pszBuf = buf;
	uBufLen = MAX_BUF_LEN;
	Message msg1_t = {{0}};
	ClientID id;
	unsigned int uMsgSize = MAX_MSG_SIZE;
	ret = unpack_msg(pszBuf, MAX_BUF_LEN, &offset, &id, &msg1_t, &uMsgSize);
	if (0 > ret)
	{
		assert(!"unpack_msg msg1 failed!");
	}
	printf("msg1 clientID = %u\n", id);
	printf("msg1_t.header.msgID=%u\n", msg1_t.header.msgID);
	printf("msg1_t.body.turnOn=%u\n", msg1_t.body.msgActivateRotate.turnOn);
	printf("msg1_t.body.degree=%u\n", msg1_t.body.msgActivateRotate.degree);

	pszBuf += offset;
	uBufLen -= offset;
	Message msg2_t = {{0}};
	ret = unpack_msg(pszBuf, MAX_BUF_LEN, &offset, &id, &msg2_t, &uMsgSize);
	if (0 > ret)
	{
		assert(!"unpack_msg msg2 failed!");
	}
	printf("msg2 clientID = %u\n", id);
	printf("msg2_t.header.msgID=%u\n", msg2_t.header.msgID);
	printf("msg2_t.body.turnOn=%u\n", msg2_t.body.msgActivateRotate.turnOn);
	printf("msg2_t.body.degree=%u\n", msg2_t.body.msgActivateRotate.degree);

}


//#endif
